//
//  VehicleDataViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/11/23.
//  Copyright © 2017年 Shenzhen Xinwo Transport. All rights reserved.
//

#import "VehicleDataViewController.h"
#import "AreaDetailViewController.h"
#import "CarDetailViewController.h"

#import "AttributionAreaView.h"
#import "BelongingOwnerView.h"
#import "VehicleUseView.h"
#import "VehicleListView.h"

#import "MineViewController.h"
#import "UIImage+clip.h"
#import "SegView.h"
#import "SearchHistoryView.h"

@interface VehicleDataViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIView *canvasView;
@property (nonatomic,strong)AttributionAreaView *attView;
@property (nonatomic,strong)BelongingOwnerView *ownerView;
@property (nonatomic,strong)VehicleUseView *useListView;
@property (nonatomic,strong)VehicleListView *veListView;
@property (nonatomic,strong)UIScrollView *contentScrollView;
@property (nonatomic,strong)SegView *segView;
@property (nonatomic,assign)BOOL viewIsLoad;
@end

@implementation VehicleDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _viewIsLoad = NO;
    [self setupNavigationTitleWithTitle:@"车辆数据"];
    [self loadUI];
    [self setupUIScrollView];
    [self.segView segmentedControlDidSelectWithIndex:0];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_viewIsLoad) {
        [self.ownerView loadData];
        _viewIsLoad = YES;
    }
}

#pragma mark - UI设置
- (void)loadUI{
    _segView = [SegView new];
    [self.view addSubview:_segView];
    [_segView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(NavationBarHeight);
        make.height.mas_equalTo(40);
    }];
    WEAKSELF
    _segView.segSelectIndex = ^(NSInteger index) {
        [weakSelf SectionSelectIndex:index];
        
    };
    //设置导航栏左右按钮
    [self setLeftBarButtonItemWithNormalImageName:@"searchNormal" andSelectImageName:@"searchSel"];
    [self setRightBarButtonItemWithNormalImageName:@"personNormal" andSelectImageName:@"personSel"];
}

- (void)setupUIScrollView {
    Kweaself
    self.contentScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:_contentScrollView];
    _contentScrollView.frame = CGRectMake(0, NavationBarHeight+40, kScreenWidth, kScreenHeight-NavationBarHeight-40);
    _contentScrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.bounces = NO;
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.delegate = self;
    _canvasView = [UIView new];
    [_contentScrollView addSubview:_canvasView];
    _canvasView.frame = CGRectMake(0, 0, kScreenWidth * 4, kScreenHeight-NavationBarHeight-40);
    
    
//    _attView = [AttributionAreaView new];
//    _attView.pushToOtherControllerWithUserID = ^(NSString *userID,NSString *title) {
//        AreaDetailViewController *areaVC = [AreaDetailViewController new];
//        areaVC.titleStr = title;
//        areaVC.areaRegionId = userID;
//        [areaVC setHidesBottomBarWhenPushed:NO];
//        [weakSelf.navigationController pushViewController:areaVC animated:YES];
//    };
//    [_canvasView addSubview:_attView];
//    [_attView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_canvasView.mas_left).offset(kScreenWidth*0);
//        make.bottom.top.mas_equalTo(_canvasView);
//        make.width.mas_equalTo(kScreenWidth);
//    }];
    _ownerView = [BelongingOwnerView new];
    [_canvasView addSubview:_ownerView];
    _ownerView.searchWithPlat = ^(NSString *deptId) {
        CarDetailViewController *carDetaliVC= [CarDetailViewController new];
        carDetaliVC.deptId = deptId;
        [carDetaliVC loadDataWithDeptId:deptId];
        [weakSelf.navigationController pushViewController:carDetaliVC animated:YES];
    };
    [_ownerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*0);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];

    _useListView = [VehicleUseView new];
    _useListView.searchWithUserId = ^(NSInteger useId) {
        CarDetailViewController *carDetaliVC= [CarDetailViewController new];
        carDetaliVC.carUseIn = useId;
        [carDetaliVC loadDataWithCarUseIn:useId];
        [weakSelf.navigationController pushViewController:carDetaliVC animated:YES];
    };
    [_canvasView addSubview:_useListView];
    [_useListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*1);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];

    _veListView = [VehicleListView new];
    [_canvasView addSubview:_veListView];
    [_veListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*2);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
}



//左按钮点击事件
- (void)leftItemBtnClick:(UIButton *)sender
{
    [super leftItemBtnClick:sender];
    [self jumpProhibit];
    SearchHistoryView *searchHistoryView = [SearchHistoryView new];
    Kweaself
    searchHistoryView.searchWithPlat = ^(NSString *palte) {
        CarDetailViewController *carDetaliVC= [CarDetailViewController new];
        carDetaliVC.plate = palte;
        [carDetaliVC loadDataWithPlat:palte];
        
        [weakSelf.navigationController pushViewController:carDetaliVC animated:YES];
    };
    [self.tabBarController.view addSubview:searchHistoryView];
    [searchHistoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.view);
    }];
}

//右按钮点击事件
- (void)rightItemBtnClick:(UIButton *)sender
{
    [super rightItemBtnClick:sender];
    sender.enabled = NO;
    MineViewController *mineVc = [MineViewController new];
    [mineVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:mineVc animated:YES];
    sender.enabled = YES;
}
//发通知允许tabBar跳转
- (void)allowProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"allowProhibit" object:nil];
}
//发通知禁止tabBar跳转

- (void)jumpProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpProhibit" object:nil];
}

- (void)dealloc
{
    
}
#pragma mark - 分栏滑动换页效果

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self.segView segmentedControlDidSelectWithIndex:index];
    [self loadSbuViewDataWithIndex:index];
}

- (void)SectionSelectIndex:(NSInteger)selectIndex {
    CGFloat offsetX = selectIndex * self.view.frame.size.width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    [self loadSbuViewDataWithIndex:selectIndex];
}

#pragma mark - 数据加载
- (void)loadSbuViewDataWithIndex:(NSInteger)index
{
    switch (index) {
//        case 0:
//            [_attView loadData];
//            break;
        case 0:
            [_ownerView loadData];
            break;
        case 1:
            [_useListView loadData];
            break;
        case 2:
            [_veListView loadData];
            break;
        default:
            break;
    }
}

@end
